<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Template_File extends JFX_File
{
	public $block;
	public $elements = array();
	public $lang = array();
	public $extra = array();

	public function __construct(){
		parent::__construct();
		$langs = JFX_Lang::getInstalledLangs();
		foreach($langs as $row){
			$this->lang[$row['keyname']] = array(
				'heading' => 'Template Heading'
			);
		}
	}

	public function setBlock(JFX_Template $block){
		$this->block = $block;
	}

	public function export($filename){

		$hookParams = array(
			'lang' => &$this->lang,
			'block' => &$this->block
		);

		JFX::hook('JFX_Template_File.export', $hookParams);


		foreach($this->block->elements as $k=>$block){
			$exportFilename = $filename.'_'.$block->id.'.php';
			if($block->isBlock){
				$exportFile = new JFX_Block_File;
			}else if($block->isTemplate){
				$exportFile = new JFX_Template_File;
			}else if($block->isPage){
				$exportFile = new JFX_Page_File;
			}
			$exportFile->setBlock($block);
			$exportFile->export($exportFilename);
			$this->elements[$k] = unserialize(file_get_contents($exportFilename));
			unlink($exportFilename);
		}

		$this->params['type'] = get_class($this);
        $this->params['title'] = $this->block->title;
        $this->params['icon_data'] = '';
        $this->params['description'] = 'Exported block '.$this->block->title;
        $this->params['created_tstamp'] = time();
        $this->params['modified_tstamps'] = array();
        $this->params['module'] = 'core';
        $this->params['method'] = 'importBlock';
        $this->params['url'] = '';
        $this->params['module_minimum_version'] = '';
        $this->params['module_maximum_version'] = '';
        $this->params['core_minimum_version'] = '';
        $this->params['core_maximum_version'] = '';
        $this->params['theme_minimum_version'] = '';
        $this->params['theme_maximum_version'] = '';
		$this->params['content'] = '';
        $this->params['auto_install'] = false;

		$fh = fopen($filename, 'w');
		fwrite($fh, serialize($this));
		fclose($fh);

	}

	public function import($parentPageId, $workspaceId, $parentBlockId=''){
		$SETTINGS = JFX::registry('JFX_Settings');
		$newBlock = clone $this->block;
		$newBlock->setDetails('page_id', $SETTINGS->get('template_page_id'));
		list($prefix, $id, $module, $instance) = explode('-', $newBlock->id);
		$newBlockId = JFX_Block::generateBlockId($SETTINGS->get('template_page_id'), $module, LIVE_WORKSPACE_ID);
		if($parentBlockId==''){
			$parentBlockId = $newBlock->getDetails('parent_id');
		}
		$newBlock->setDetails(array(
			'id' => $newBlockId,
			'parent_id' => $parentBlockId,
			'page_id' => $SETTINGS->get('template_page_id'),
			'workspace_id' => LIVE_WORKSPACE_ID
		));
		$newBlock->save();

		$hookParams = array(
			'lang' => &$this->lang,
			'block' => &$newBlock
		);

		JFX::hook('JFX_Template_File.import', $hookParams);

		foreach($this->elements as $k=>$block){
			$block->import($parentPageId, $workspaceId, $newBlock->getDetails('id'));
		}

		return $newBlockId;

	}

	public static function loadFromFile($filename){
		return unserialize(file_get_contents($filename));
	}

}

